$(function () {

    $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm': 'cd'}, function (cddata) {
        $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm': 'jzfs'}, function (data) {
            sskcGdGrid($.webPath() + '/kcgl/sskc/sskcGd_wzmx?khIds=' + $("#sskcGd_khIds").val() || '', cddata, data);
        })
    });

})
function sskcGdGrid(href, cdOptions, fsOptions) {
    $("#sskcGd_mx").edatagrid({
        url: href,
        fitColumns: true,
        singleSelect: true,
        fit: false,
        width: 1000,
        height: 390,
        checkOnSelect: false,
        selectOnCheck: false,
        showFooter: false,
        rownumbers: true,
        columns: [
            [
                {field: 'wzgg.code', title: '物资编码', width: 100},
                {field: 'wzgg.name', title: '物资名称', width: 100},
                {field: 'wzgg.ggxh', title: '规格型号', width: 100},
                {field: 'wzgg.dw', title: '单位', width: 60},
                {field: 'wsdj', title: '成本单价(不含税)', formatter: function (value) {
                    var sl = formatJe(value, ",");
                    return sl;
                }, width: 100},
                {field: 'cd.z', title: '产地', width: 50},
//                {field:'chd', title:'长度', width:50},
//                {field:'jz', title:'件重', width:50},
                {field: 'kkdsl', title: '可供重量', formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }, width: 100},
                {field: 'gdjs', editor: {type: 'numberbox',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value);
                        return sl;
                    }}, title: '挂单件数', width: 50},
                {field: 'gdsl', editor: {type: 'numberbox', options: {min: 0, precision: 4}}, formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }, title: '挂单重量', width: 100}
            ]
        ],
        loadFilter: function (data) {
            $(data.rows).each(function () {
                this.gdjs = (this.jz == null || this.jz == 0) ? this.js : parseInt(this.kkdsl / this.jz);
                this.gdsl = this.kkdsl;
            })
            return data;
        },
        onSelect: function (rowIndex) {
            //获取每个单元格
            $("#sskcGd_mx").edatagrid('editRow', rowIndex);
            var editors = $("#sskcGd_mx").datagrid('getEditors', rowIndex);
            if (editors.length == 0)
                return;
            var gdjsEditor = editors[0];
            var gdslEditor = editors[1];
            gdjsEditor.target.unbind('blur').bind('blur', function () {
                $(gdjsEditor.target).numberbox('setValue', $(gdjsEditor.target).val());
                var selected = $('#sskcGd_mx').datagrid('getSelected');
                if (selected['cd.id'] != "" && selected) {
                    $.post($.webPath() + '/cggl/cgdgl/get_jz', {'ggId': selected['wzgg.id'], cdId: selected['cd.id']}, function (data) {
                        var obj = eval('(' + data + ')');
                        if (obj.jz && obj.jz != '') {
                            $(gdslEditor.target).numberbox('setValue', parseFloat(obj.jz) * $(gdjsEditor.target).numberbox('getValue'));
                        } else {
                            $(gdslEditor.target).numberbox('setValue', 0);
                            $.messager.alert('提示', '此物资未存单位件重，请手动填写申报重量', 'info');
                        }
                    })
                } else {
                    $.messager.alert('提示', '产地不能为空', 'info');
                }
            });
            gdslEditor.target.unbind('blur').bind('blur', function () {
                $(gdslEditor.target).numberbox('setValue', $(gdslEditor.target).val());
                $(gdjsEditor.target).numberbox('setValue', $(gdjsEditor.target).val());
            });
        }
    })
}

//计算开单重量和开单金额
function countlldSum(index, count, je) {
    var sumkdsl = 0;
    var sumkddj = 0;
    var data = $("#lld_mx").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var kdsl = parseFloat(data.rows[i]['kdsl']);
        var kddj = parseFloat(data.rows[i]['kddj']);
        if (i == index) {
            kdsl = parseFloat(count);
            kddj = parseFloat(je);
        } else {
            kdsl = isNaN(kdsl) ? 0 : kdsl;
            kddj = isNaN(kddj) ? 0 : kddj;
        }
        sumkdsl += kdsl;
        sumkddj += kddj;
    }
    addFooter(sumkdsl, sumkddj);
}

//在表底加载开单重量和开单金额的合计
function addFooter(sumkdsl, sumkddj) {
    var footer = [
        {"wzgg.code": "合计", "kdsl": sumkdsl, "kddj": sumkddj}
    ];
    if (footer != null)
        $("#lld_mx").edatagrid('reloadFooter', footer);
}

